// 营销倒计时behavior,当页面隐藏时暂停计时

const {
  padStart,
  isEmpty
} = require("../../../js/newUtil")

module.exports = Behavior({
  behaviors: [],
  data: {},
  lifetimes: {
    attached() {
      // this.startMarketInterval()
    }
  },
  pageLifetimes: {
    show() {
      this.startMarketInterval()
    },
    hide() {
      this.clearMarketInterval()
    },
  },
  methods: {
    startMarketInterval() {
      const STEP = 1000
      clearInterval(this.marketInterval)
      this.marketInterval = setInterval(() => {
        let widgets = this.data.widgets || []
        if (widgets.length > 0) {
          for (let i = 0; i < widgets.length; i++) {
            let widget = widgets[i]
            if (widget.WidgetName == 'PanicBuy' || widget.WidgetName == 'GroupBuy' || widget.WidgetName == 'PreSale') {
              let data = widget.Datas || []
              for (let j = 0; j < data.length; j++) {
                let item = data[j] || {}
                let isStart = false
                let flag = widget.WidgetName
                if (flag == 'PanicBuy') {
                  isStart = item.IsStart == 1
                } else {
                  isStart = item.GroupStatus == 2
                }
                if (isStart) {
                  let end = new Date(item.EndTime)
                  let timer = {
                    c0: false
                  }
                  if (isNaN(end.getFullYear())) {
                    timer = {
                      c0: false
                    }
                  } else {
                    let diff = end.getTime() - Date.now()
                    let dateData = {
                      years: 0,
                      days: 0,
                      hours: 0,
                      min: 0,
                      sec: 0,
                      millisec: 0,
                    }
                    if (diff < 0) {
                      timer = {
                        c0: false
                      }
                    } else {
                      diff = diff / 1000
                      if (diff >= 86400) {
                        dateData.days = Math.floor(diff / 86400)
                        diff -= dateData.days * 86400
                      }
                      if (diff >= 3600) {
                        dateData.hours = Math.floor(diff / 3600)
                        diff -= dateData.hours * 3600
                      }
                      if (diff >= 60) {
                        dateData.min = Math.floor(diff / 60)
                        diff -= dateData.min * 60
                      }
                      dateData.sec = Math.round(diff)
                      dateData.days = padStart(dateData.days, 2, '0')
                      dateData.hours = padStart(dateData.hours, 2, '0')
                      dateData.min = padStart(dateData.min, 2, '0')
                      dateData.sec = padStart(dateData.sec, 2, '0')
                      if (dateData.days == '00' && dateData.hours == '00' && dateData.min == '00' && dateData.sec == '00') {
                        timer = {
                          c0: false
                        }
                      } else {
                        timer = {
                          c0: true,
                          c1: dateData.days,
                          c2: dateData.hours,
                          c3: dateData.min,
                          c4: dateData.sec,
                        }
                      }
                    }
                  }
                  data[j].time = timer
                }
              }
              this.setData({
                [`widgets[${i}].Datas`]: data
              })
            }
          }
        }
      }, STEP)
    },
    clearMarketInterval() {
      if (this.marketInterval) {
        clearInterval(this.marketInterval)
      }
    }
  }
})